﻿//#define  _CRT_SECURE_NO_WARNINGS 1
#include "Snake.h"

void CreatMap()
{
	//上
	SetPos(0, 0);
	for (int i = 0; i <= 56; i += 2)
	{
		//这是打印宽字符
		wprintf(L"%lc", WALL);
	}
	//下
	SetPos(0, 26);
	for (int i = 0; i <= 56; i += 2)
	{
		//这是打印宽字符
		wprintf(L"%lc", WALL);
	}
	//左
	for (int i = 1; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}
	//右
	for (int i = 1; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
}

void InitSnake(pSnake ps)
{
	pSnakeNode cur = NULL;
	int i = 0;
	//创建蛇身节点，并初始化坐标
	//头插法
	for (i = 0; i < 5; i++)
	{
		//创建蛇身的节点
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake()::malloc()");
			return;
		}
		//设置坐标
		cur->next = NULL;
		cur->x = POS_X + i * 2;
		cur->y = POS_Y;

		//头插法
		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;
		}
		else
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
	}

	//打印蛇的身体
	cur = ps->_pSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}

	//初始化贪吃蛇数据
	ps->_SleepTime = 200;
	ps->_Socre = 0;
	ps->_Status = OK;
	ps->_Dir = RIGHT;
	ps->_Socre = 10;
}

void SetPos(short x, short y)
{
	COORD pos = { x,y };
	HANDLE hOutput = NULL;

	//获取标注输出的句柄(用来识别不同设备的数值)
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//设置标准输出上光标的位置pos
	SetConsoleCursorPosition(hOutput, pos);
}

void WelComeToGame()
{
	SetPos(40, 14);
	printf("欢迎来到贪吃蛇游戏");
	SetPos(40, 25);
	system("pause");
	system("cls");
	SetPos(20, 14);
	printf("用↑，↓，←，→分别控制蛇的移动,F3是加速，F4是减速\n");
	system("pause");
	system("cls");
}

void CreatFood(pSnake ps)
{
	int x = 0;
	int y = 0;
again:
	do
	{
		x = rand() % 53 + 2; // 2 - 54
		y = rand() % 25 + 1; // 1 - 25
	} while (x % 2 != 0); // x的坐标必须是2的倍数

	//坐标不能和蛇的身体重合
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		//比较坐标
		if (cur->x == x && cur->y == y)
		{
			goto again;
		}
		cur = cur->next;
	}

	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(pSnake));
	if (pFood == NULL)
	{
		perror("CreatFood()::malloc");
		exit(2);
	}
	pFood->x = x;
	pFood->y = y;
	SetPos(pFood->x, pFood->y);
	wprintf(L"%c", FOOD);
	ps->_pFood = pFood;
}

//打印帮助信息
void PrintHelpInfo()
{
	SetPos(64, 15);
	printf("1.不能撞墙，不能咬到自己");
	SetPos(64, 16);
	printf("2.用↑，↓，←，→分别控制蛇的移动");
	SetPos(64, 17);
	printf("3.F3:加速 F4:减速");
	SetPos(64, 18);
	printf("4.Esc:退出 空格: 暂停游戏");
}

void Pause()
{
	while (1)
	{
		Sleep(100);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}

int NextIsFood(pSnake ps, pSnakeNode pNext)
{
	return (pNext->x == ps->_pFood->x) && (pNext->y == ps->_pFood->y);
}

void EatFood(pSnake ps, pSnakeNode pnext)
{
	//头插法+更新头节点
	pnext->next = ps->_pSnake;
	ps->_pSnake = pnext;

	pSnakeNode cur = ps->_pSnake;

	//打印蛇
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}

	free(ps->_pFood); //释放掉食物
	ps->_Socre += ps->_FoodWeight; //加分

	//创建随便位置的食物
	CreatFood(ps);
}

void NoFood(pSnake ps,pSnakeNode pnext )
{
	//头插法
	pnext->next = ps->_pSnake;
	ps->_pSnake = pnext;
	pSnakeNode cur = ps->_pSnake;
	//找到倒数第二个节点，打印蛇
	while (cur->next->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}

	//最后一个位置打印空格，然后释放节点
	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	free(cur->next);
	cur->next = NULL;
}

void KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if ((ps->_pSnake->x == cur->x)
			&& (ps->_pSnake->y == cur->y))
		{
			ps->_Status = KILL_BY_SELF;
		}
		cur = cur->next;
	}
}

void KillByWall(pSnake ps)
{
	if ((ps->_pSnake->x == 0)
		|| (ps->_pSnake->x == 56)
		|| (ps->_pSnake->y == 0)
		|| (ps->_pSnake->y == 26))
	{
		ps->_Status = KILL_BY_WALL;
	}
}

void SnakeMove(pSnake ps)
{
	pSnakeNode pNext = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNext == NULL)
	{
		perror("SnackMove::malloc");
		exit(2);
	}
	pNext->next = NULL;


	switch (ps->_Dir)
	{
	case UP:
		pNext->x = ps->_pSnake->x;
		pNext->y = ps->_pSnake->y - 1;
		break;
	case DOWN:
		pNext->x = ps->_pSnake->x;
		pNext->y = ps->_pSnake->y + 1;
		break;
	case LEFT:
		pNext->x = ps->_pSnake->x - 2;
		pNext->y = ps->_pSnake->y;
		break;
	case RIGHT:
		pNext->x = ps->_pSnake->x + 2;
		pNext->y = ps->_pSnake->y;
		break;
	}

	//判断是否可能遇到了食物
	if (NextIsFood(ps, pNext))
	{
		//吃食物,将节点连接起来
		EatFood(ps,pNext);
	}
	else
	{
		//如果不是食物，就将尾节点释放掉
		NoFood(ps,pNext);
	}

	//蛇是否撞墙
	KillByWall(ps);
	
	//蛇是否自杀
	KillBySelf(ps);
}

void GameStart(pSnake ps)
{
	//设置控制台窗口大小，30行，100列
	system("mode con cols=100 lines=30");
	//设置cmd窗口名称
	system("title 贪吃蛇");

	//获得标准输出的句柄（用来识别不同设备的数值）
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

	//这个函数可以获取光标的大小和可见性
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(hOutput, &CursorInfo);//获取控制台光标信息
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(hOutput, &CursorInfo); // 设置控制台光标状态,隐藏光标
	//printf("光标默认大小: %d 光标是否可见: %d\n", CursorInfo.dwSize, CursorInfo.bVisible);

	//欢迎界面
	WelComeToGame();

	//创建地图
	CreatMap();

	//初始化贪吃蛇
	InitSnake(ps);

	//创建食物
	CreatFood(ps);
}

void GameRun(pSnake ps)
{
	PrintHelpInfo();
	do
	{
		SetPos(64, 10);
		printf("得分: %05d", ps->_Socre);
		SetPos(64, 11);
		printf("每个食物的得分: %d", ps->_FoodWeight);
		if (KEY_PRESS(VK_UP) && ps->_Dir != DOWN)
		{
			ps->_Dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_Dir != UP)
		{
			ps->_Dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_Dir != RIGHT)
		{
			ps->_Dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_Dir != LEFT)
		{
			ps->_Dir = RIGHT;
		}
		else if (KEY_PRESS(VK_ESCAPE)) // 按了ESC,退出
		{
			ps->_Status = END_NOMAL;
			break;
		}
		else if (KEY_PRESS(VK_SPACE)) //按了空格，说明暂停
		{
			Pause();
		}
		else if (KEY_PRESS(VK_F3)) //加速
		{
			//走的越块，食物的得分越多
			if (ps->_SleepTime >= 80)
			{
				ps->_SleepTime -= 30;
				ps->_FoodWeight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4)) //减速
		{
			//同理，食物得分越少
			if (ps->_SleepTime < 320)
			{
				ps->_FoodWeight += 30;
				ps->_SleepTime -= 2;
			}
		}

		Sleep(ps->_SleepTime);
		SnakeMove(ps);

	} while (ps->_Status == OK);


}

void GameEnd(pSnake ps)
{
	SetPos(20, 12);
	switch (ps->_Status)
	{
	case END_NOMAL:
		printf("主动退出游戏\n");
		break;
	case KILL_BY_SELF:
		printf("游戏自杀\n");
		break;
	case KILL_BY_WALL:
		printf("游戏撞墙\n");
		break;
	}

	//释放节点
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}
}
